package com.foreveredu.propms.web.struts.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.domain.Role;
import com.foreveredu.propms.domain.User;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.service.IPropQueryService;
import com.foreveredu.propms.web.struts.actionform.DistYearQueryActionForm;
import com.foreveredu.propms.web.util.ExportExcelUtils;

public class DistYearQueryAction extends AbstractBaseDispatchAction {
	
	private IManagementService managementService = null;
	private IPropQueryService propQueryService = null;
	private List<Map<String, Object>> sumQueryList;
	private int year;
	   
	// 获得日志记录器
	Logger log = Logger.getLogger(AccountAction.class);

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public IPropQueryService getPropQueryService() {
		return propQueryService;
	}

	public void setPropQueryService(IPropQueryService propQueryService) {
		this.propQueryService = propQueryService;
	}
	
	// 跳转到物业服务处收费年汇总查询条件页面
	public ActionForward queryDistYearCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得所有的物业服务处
		List<District> distList = managementService.findDistricts();
		request.setAttribute("distList", distList);
		return mapping.findForward("query_distYearCharge");
	}
	
	//执行物业服务处年汇总查询
	public ActionForward doQueryDistYearCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//获得表单数据
		DistYearQueryActionForm dyqform = (DistYearQueryActionForm) form;
		int distId = dyqform.getDistId();
		year = dyqform.getYear();
		
		sumQueryList = new ArrayList<Map<String,Object>>();
		//当管理员不选择物业服务处，则查询所有物业服务处当年物业费
		if(distId == 0){
			//获得所有的物业服务区
			List<District> distList = managementService.findDistricts();
			for(District dist:distList){
				sumQueryList.add(propQueryService.queryDistYearCharge(dist, year));
			}	
		}else{
			//根据distId来查询业务服务区的信息
			District district = managementService.findDistrictById(distId);
			//根据业务服务处ID来查询下面所有小区的当年物业费
			sumQueryList.add(propQueryService.queryDistYearCharge(district, year));
		}
		request.setAttribute("sumQueryList", sumQueryList);
		request.setAttribute("year", year);
		
		return mapping.findForward("doQuery_distYearCharge");
	}
	
	//将数据生成到Excel表格中并导出
	public ActionForward doExportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		try{
			response.setContentType("octets/stream"); 
			//设置表名
			String fileName="物业服务处收费年汇总("+year+"年)";
			response.addHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("GBK"),"ISO8859-1")+".xls"); 
			
            OutputStream out = response.getOutputStream();
            
            //设置表格中的字段名
            String[] headers = {"服务处", "应缴金额", "实收金额","物业服务费","电费","水费","停车服务费","垃圾清运费","路灯电费","二次提水费"};
            //设置Key，然后通过这个Key去Map数组中取值
            String[] columns ={"distName","totalSum","paidSum","propCharge","powerCharge","waterCharge","carCharge","rubbishCharge","lightCharge","water2ndCharge"};
            
            ExportExcelUtils.exportExcel("物业服务处月汇总表", headers, columns, sumQueryList, out, "");
            out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}










